package is.glif.fenius.client.connection;

import java.util.List;

import is.glif.fenius.ws.connection.gen.Avp;
import is.glif.fenius.ws.connection.gen.AvpSeq;
import is.glif.fenius.ws.connection.gen.Edge;
import is.glif.fenius.ws.connection.gen.Reservable;
import is.glif.fenius.ws.connection.gen.Reservation;
import is.glif.fenius.ws.connection.gen.Schedule;
import is.glif.fenius.ws.connection.gen.Topology;
import is.glif.fenius.ws.connection.gen.Vertex;

public class StringOutputter {
    public static String printReservable(Reservable res) {
        String out = "";
        Schedule s = res.getSchedule();
        Topology t = res.getTopology();
        List<String> descs = res.getDescription();
        List<String> tags = res.getTag();
        AvpSeq params = res.getAdhocParams();
        
        if (s.getAdvance()!= null) {
            out += "schedule: advance\n";
            out += "  start: "+s.getAdvance().getStart()+"\n";
            out += "  end: "+s.getAdvance().getEnd()+"\n";
            out += "  dur: "+s.getAdvance().getDuration()+"\n";
        } else if (s.getImmediate() != null) {
            out += "schedule: immediate\n";
        }
        out += printAvpSeq(params, "");
        
        for (String desc : descs) {
            out += "desc: "+desc+"\n";
        }
        for (String tag : tags) {
            out += "tag: "+tag+"\n";
        }
        
        for (Vertex v : t.getVertices()) {
            out += "vertex: "+v.getVertexId()+"\n";
            out += "  urn: "+v.getResourceId()+"\n";
            out += "  desc: "+v.getDescription()+"\n";
            out += "  adhocParams:\n";
            out += printAvpSeq(v.getAdhocParams(), "    ");
            out += "  techParams:\n";
            out += printAvpSeq(v.getTechParams(), "    ");
        }
        for (Edge e : t.getEdges()) {
            out += "edge: "+e.getEdgeId()+"\n";
            out += "  bw: "+e.getBandwidth()+"\n";
            out += "  desv: "+e.getDescription()+"\n";
            out += "  a: "+e.getA()+" --- "+e.getZ()+"\n";
            out += "  adhocParams:\n";
            out += printAvpSeq(e.getAdhocParams(), "    ");
        }
        

        
        return out;
    }
    public static String printReservation(Reservation res) {
        String out = "";
        String id = res.getReservationId();
        out += "id: "+id+"\n";
        Schedule s = res.getSchedule();
        Topology t = res.getTopology();
        List<String> descs = res.getDescription();
        List<String> tags = res.getTag();
        AvpSeq params = res.getAdhocParams();
        out += "  actStat: "+res.getActivationStatus()+"\n";
        out += "  rsvStat: "+res.getReservationStatus()+"\n";
        
        
        if (s.getAdvance()!= null) {
            out += "schedule: advance\n";
            out += "  start: "+s.getAdvance().getStart()+"\n";
            out += "  end: "+s.getAdvance().getEnd()+"\n";
            out += "  dur: "+s.getAdvance().getDuration()+"\n";
        } else if (s.getImmediate() != null) {
            out += "schedule: immediate\n";
        }
        out += printAvpSeq(params, "");
        
        for (String desc : descs) {
            out += "desc: "+desc+"\n";
        }
        for (String tag : tags) {
            out += "tag: "+tag+"\n";
        }
        
        for (Vertex v : t.getVertices()) {
            out += "vertex: "+v.getVertexId()+"\n";
            out += "  urn: "+v.getResourceId()+"\n";
            out += "  desc: "+v.getDescription()+"\n";
            out += "  adhocParams:\n";
            out += printAvpSeq(v.getAdhocParams(), "    ");
            out += "  techParams:\n";
            out += printAvpSeq(v.getTechParams(), "    ");
        }
        for (Edge e : t.getEdges()) {
            out += "edge: "+e.getEdgeId()+"\n";
            out += "  bw: "+e.getBandwidth()+"\n";
            out += "  desv: "+e.getDescription()+"\n";
            out += "  a: "+e.getA()+" --- "+e.getZ()+"\n";
            out += "  adhocParams:\n";
            out += printAvpSeq(e.getAdhocParams(), "    ");
        }
        

        
        return out;
    }
    
    
    private static String printAvpSeq(AvpSeq avpSeq, String indent) {
        String out = "";
        if (avpSeq == null) return "";
        for (Avp avp : avpSeq.getAvp()) {
            out += indent+"avp : "+avp.getAttrName()+" :: "+avp.getAttrVal()+"\n";
        }

        return out;
    }
}
